package com.alks.function.service.async;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alks.common.data.dto.SysUserTokenDTO;
import com.alks.common.utils.beanutils.ArrayUtils;
import com.alks.common.utils.beanutils.ObjectUtils;
import com.alks.common.utils.export.constants.FileTypeEnum;
import com.alks.common.utils.export.service.FileExportTask;
import com.alks.entity.data.entity.PcLotDetail;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.request.FormingFeedingRequest;
import com.alks.function.data.request.factoryquery.StitchHeadRequest;
import com.alks.function.data.vo.biocontrol.deptwork.ReportStitchDailyVO2;
import com.alks.function.data.vo.produce.ReportMonthVo;
import com.alks.function.download.StitchStkExport;
import com.alks.function.mapper.FormingProductionMapper;
import com.alks.function.mapper.pcfactoryquery.FT_QR_PcStitchDetailMapper;
import com.alks.function.mapper.stockdept.pcfactorystockquery.SD_SQ_PcLotDetailMapper;
import com.alks.function.service.impl.pcfactoryquery.PcStitchDetailServiceImpl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author: chen
 * @date: 2025/1/8
 * @description:
 */
@Component
public class FileExportAsyncService {


    @Async
    public void doExport(FormingFeedingRequest request, SysUserTokenDTO userTokenDTO) {
        // 生成文件ID
        String taskFileId = FileExportTask.startExport(userTokenDTO.getUserId(),
                "成型月报表" + StrUtil.DOT + FileTypeEnum.XLSX.getValue());

        FormingProductionMapper formingProductionMapper = SpringUtil.getBean(FormingProductionMapper.class);
        // 投料明细
        List<ReportMonthVo> putIntoList = formingProductionMapper.getMonthlyReport(request.getStartTime(), userTokenDTO.getCompanyId());
        // 产出明细
        List<ReportMonthVo> outPutLIst = formingProductionMapper.getMonthlyReport2(request.getStartTime(), userTokenDTO.getCompanyId());
        if (ObjectUtils.isEmpty(putIntoList) || ObjectUtils.isEmpty(outPutLIst)){
            throw new ServiceErrorException("查询月数据为空,不可导出!");
        }

        FileExportTask fileExportTask = new LastFileExport(request.getStartTime(), putIntoList, outPutLIst);
        fileExportTask.export(userTokenDTO.getUserId(), taskFileId);

    }

    @Async
    public void doStitchStkExport(StitchHeadRequest request, SysUserTokenDTO userTokenDTO) {
        /*生成文件ID*/
        String taskFileId = FileExportTask.startExport(userTokenDTO.getUserId(),
                "荣信针车日报表" + StrUtil.DOT + FileTypeEnum.XLSX.getValue());
        /*获取针车相关数据*/
        FT_QR_PcStitchDetailMapper stitchDetailMapper = SpringUtil.getBean(FT_QR_PcStitchDetailMapper.class);
        List<ReportStitchDailyVO2> stitchList = stitchDetailMapper.stitchStkExportQuery(request,userTokenDTO.getCompanyId());
        if (ArrayUtils.isEmpyt(stitchList)){
            throw new ServiceErrorException("暂无导出数据");
        }
        List<String> lotNoList = stitchList.stream()
                .map(ReportStitchDailyVO2::getLotNo)
                .filter(Objects::nonNull).distinct()
                .collect(Collectors.toList());
        /*获取订单补充数据*/
        SD_SQ_PcLotDetailMapper lotDetailMapper = SpringUtil.getBean(SD_SQ_PcLotDetailMapper.class);
        List<PcLotDetail> lotDetailList = lotDetailMapper.getByLotNo(lotNoList,userTokenDTO.getCompanyId());
        StitchStkExport stitchStkExport = new StitchStkExport(stitchList,lotDetailList);
        stitchStkExport.export(userTokenDTO.getUserId(), taskFileId);
    }
}
